#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
vector<int> v;
int a, b, q, l, r;
int gg;

int check(int mid)
{
    if (v[mid] >= l && v[mid] <= r)
        return 0;
    if (v[mid] < l)
        return -1;
    else
        return 1;
}

int main()
{
    cin >> a >> b >> q;
    gg = gcd(a, b);
    // 对最大公约数分解质因数
    // 直接把所有约数求出来就好了：时间复杂度：O(根号n)
    for (int i = 1; i <= gg / i; ++i)
    {
        if (gg % i == 0)
        {
            v.push_back(i);
            if (gg / i != i)
                v.push_back(gg / i);
        }
    }
    sort(v.begin(), v.end());
    while (q--)
    {
        cin >> l >> r;
        // 因为所有约数都有了，并且从小到大排序，所以可以用二分
        int ll = 0, rr = v.size() - 1;
        while (ll < rr)
        {
            int mid = (ll + rr + 1) >> 1;
            int ret = check(mid);
            if (ret == 0)
                ll = mid;
            else if (ret == 1)
                rr = mid - 1;
            else
                ll = mid + 1;
        }
        if (check(ll) == 0)
            cout << v[ll] << endl;
        else
            cout << -1 << endl;
    }
    return 0;
}